﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using lauimini_webform.Data;
using lauimini_webform.Models;


namespace lauimini_webform.BLL
{
    public class UserService
    {

        User user = new User();
        MyDBContext context = new MyDBContext();
        public bool addUser(string userName, string userAccount, string password)
        {
            var user = context.users.FirstOrDefault(x => x.userAccount == userAccount);
            if (user != null)
            {
                return false;
            }
            User user1 = new User();
            user1.userName = userName;
            user1.userAccount = userAccount;
            user1.password = password;
            try
            {
                context.users.Add(user1);
                context.SaveChanges();
                return true;
            }
            catch (Exception)
            {

                throw;
            }


        }

        /// <summary>
        /// 返回0为未找到该用户，返回1成功且密码正确，返回2则密码不正确
        /// </summary>
        /// <param name="userAccount"></param>
        /// <param name="password"></param>
        public int checkUser(string userAccount, string password)
        {
            User users = context.users.Where(x => x.userAccount == userAccount).FirstOrDefault();
            if (users != null)
            {
                string dbpassword = users.password;
                if (dbpassword == password)
                {
                    return 1;
                }

                return 2;
            }
            return 0;
        }

        /// <summary>
        /// 检查是否有该用户
        /// </summary>
        /// <param name="userAccount"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool checkRepateUser(string userAccount, string password)
        {
            User users = context.users.Where(x => x.userAccount == userAccount).FirstOrDefault();
            if (users != null)
            {
                return true;


            }
            return false;
        }

        /// <summary>
        /// 获得该用户
        /// </summary>
        /// <param name="userAccount"></param>
        /// <returns></returns>
        public User GetUser(string userAccount)
        {
            User users = context.users.Where(x => x.userAccount == userAccount).FirstOrDefault();
            return users;
        }

        /// <summary>
        /// 获得该用户的id
        /// </summary>
        /// <param name="userAccount"></param>
        /// <returns></returns>
        public int getUserid(string userAccount)
        {
            User users = context.users.Where(x => x.userAccount == userAccount).FirstOrDefault();
            return users.id;

        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="userAccount"></param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool editUser(string userAccount, string userName, string password)
        {
            int userid = getUserid(userAccount);
            User user1 = context.users.FirstOrDefault(x => x.id == userid);
            user1.userName = userName;
            user1.password = password;
            context.SaveChanges();
            return true;

        }

        public List<UserTarget> GetStartTarget(string userAccount)
        {
            int userid = getUserid(userAccount);
            return context.userTargets.Where(x => x.userid == userid && x.StartTime != null&x.Status!="已完成").ToList();
        }

    }
}